package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.LocationMgr;
import ejb.dto.LocationDTO;

@ManagedBean
@RequestScoped
public class LocationBean {
	
	public LocationBean() {
		location = new LocationDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private LocationMgr locationMgr; 
	
	@SuppressWarnings("unused")
	private List<LocationDTO> allLocations;
	private int locationId;
	private LocationDTO location;
	private String errorMessage;
	
	/**
	 * Delete Location from database
	 * @param id
	 * @return redirect to page <code>manageLocations</code>
	 */
	public String deleteLocation(int id) {
		boolean result = locationMgr.deleteLocation(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageLocations?faces-redirect=true";
	}
	
	/**
	 * Update Location in database
	 * @return redirect to page <code>manageLocations</code>
	 */
	public String updateLocation() {
		boolean result = locationMgr.updateLocation(location);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageLocations?faces-redirect=true";
	}
	
	/**
	 * Insert Location in database
	 * @return redirect to page <code>manageLocations</code>
	 */
	public String insertLocation() {
		boolean result = locationMgr.insertLocation(location);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageLocations?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<LocationDTO> getAllLocations() {
		return locationMgr.getAllLocationsDTO();
	}

	public void setAllLocations(List<LocationDTO> allLocations) {
		this.allLocations = allLocations;
	}


	public int getLocationId() {
		return locationId;
	}

	public void setLocationId(int locationId) {
		this.locationId = locationId;
		location = locationMgr.getLocationDTO(locationId);
		if(location == null)
			SessionBean.notFound();
	}

	public LocationDTO getLocation() {
		return location;
	}

	public void setLocation(LocationDTO location) {
		this.location = location;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}	

	
}
